#include <stdio.h>
#include <sys/shm.h>
#include <signal.h>
#include <sys/ipc.h>

int main(void)
{

    int key = ftok("./a.c", "a");
    // int shmid = shmget(IPC_PRIVATE, 128, 0777);
    int shmid = shmget(key, 128, IPC_CREAT | 0777);
    if (shmid < 0)
    {
        printf("create share memory faile\n");;
        return -1;
    }
    printf("create success id = %d\n", shmid);

    char *p = (char *)shmat(shmid, NULL, 0); // 设为 0 表示读写权限jk
    if (p == NULL)
    {
        perror("shmat function fail");
        exit(1);
    }

    // write share memory
    fgets(p, 128, stdin);

    // start read share memory
    printf("share memory data:%s\n", p);
    printf("share memory data:%s\n", p);


    int ret = shmdt(p);
    if (ret == -1)
    {
        perror("unmap error");
        exit(1);
    }

    // printf("share memory data:%s\n", p);

    int res = shmctl(shmid, IPC_RMID, NULL);

    return 0;
}